package com.ylr.basic.common.enums;

import lombok.Getter;

import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-07-06 22:46:11
 * className: BusinessTypeEnum
 * version: 1.0
 * description:
 */
@Getter
public enum BusinessTypeEnum {

    ;

    /**
     * 值
     */
    private final Integer value;

    /**
     * 名称
     */
    private final String name;

    /**
     * 过期时间
     */
    private final Long expire;

    /**
     * 时间单位
     */
    private final TimeUnit timeUnit;

    BusinessTypeEnum(Integer value, String name) {
        this.value = value;
        this.name = name;
        this.expire = -1L;
        this.timeUnit = TimeUnit.SECONDS;
    }

    BusinessTypeEnum(Integer value, String name, Long expire, TimeUnit timeUnit) {
        this.value = value;
        this.name = name;
        this.expire = expire;
        this.timeUnit = timeUnit;
    }

    /**
     * 获取过期时间毫秒数值
     * @return 数值
     */
    public Long getExpireMillis() {
        if (Long.valueOf(-1).equals(expire)) {
            return -1L;
        }
        return timeUnit.toMillis(expire);
    }

    /**
     * 获取业务类型枚举对象
     * @param value 参数值
     * @return 业务类型枚举对象
     */
    public static BusinessTypeEnum getBusinessTypeEnum(Integer value) {
        BusinessTypeEnum[] businessTypeEnums = values();
        int lo = 0, hi = businessTypeEnums.length - 1;
        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2;
            BusinessTypeEnum businessTypeEnum = businessTypeEnums[mid];
            int res = businessTypeEnum.getValue().compareTo(value);
            if (res > 0) {
                hi = mid - 1;
            } else if (res < 0) {
                lo = mid + 1;
            } else {
                return businessTypeEnum;
            }
        }
        return null;
    }
}
